import * as constant from '../constant'
import request from "../request"
import { push } from 'connected-react-router'

export const unload = () => {
    return { type: constant.ARTICLE_UNLOAD }
}

export const articleAddTag = () => {
    return { type: constant.ARTILE_ADD_TAG }
}
export const articleRemoveTag = (tag) => {
    return { type: constant.ARTILE_REMOVE_TAG, payload: tag }
}

//字段同步
export const articleFieldUpdate = (key, value) => {
    return { type: constant.ARTICLES_UPDATE_FIELD, key, value }
}
export const registSubmit = ({ email, username, password }) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.user.regist(email, username, password)
            // 成功 status 1
            if (result.status === 1) {
                // 跳转Login ？
                // redux +  react-router
                dispatch(push('/login')) //触发跳转 //router + redux
            } else {// 失败 status 0
                dispatch({ type: constant.USER_REGIST_RESULT, result })
            }
        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.USER_REGIST_RESULT, result: { message: error.message, errors: error.errors } })
        }

    }
}
// 创建文章
export const createAtricle = (article) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.create(article)
            // 成功 status 1
            if (result.status === 1) {
                const { slug } = result.data
                dispatch(push(`/article/${slug}`)) //触发跳转 //router + redux
            } else {// 失败 status 0
                dispatch({ type: constant.ARTICLE_CREATE_RESULT, result })
            }
        } catch (error) { // 错误
            console.log(error);
            dispatch({ type: constant.ARTICLE_CREATE_RESULT, result: { message: error.message, errors: error.errors } })
        }

    }
}
// 文章详情
export const getArticleBySlug = (slug) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.get(slug)
            // 成功 status 1
            console.log('文章详情', result);
            dispatch({ type: constant.ARTICLE_GET_RESULT, result })

        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.ARTICLE_GET_RESULT, result: { message: error.message, errors: error.errors } })
        }

    }
}
// 更新文章
export const upDataAtricle = (artcile) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.update(artcile)
            if (result.status === 1) {
                console.log('更新文章', result);
                const { slug } = result.data
                dispatch(push(`/article/${slug}`)) //触发跳转 //router + redux
            } else {// 失败 status 0
                dispatch({ type: constant.ARTICLE_CREATE_RESULT, result })
            }
        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.ARTICLE_UPDATA_RESULT, result: { message: error.message, errors: error.errors } })
        }

    }
}
// 删除文章
export const deleteArticle = (slug) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.delete(slug)
            if (result.status === 1) {
                console.log('喜欢文章-action', result);
                dispatch(push(`/home`)) //触发跳转 //router + redux
            } else {// 失败 status 0
                dispatch({ type: constant.ARTICLE_DELETE_RESULT, result })
            }
        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.ARTICLE_DELETE_RESULT, result: { message: error.message, errors: error.errors } })
        }
    }
}

//不喜欢
export const unfavoriteArticle = (slug) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.unfavorite(slug)
            // console.log('不喜欢文章-action', result);
            dispatch({ type: constant.ARTICLE_FAVORITE_RESULT, result })
        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.ARTICLE_FAVORITE_RESULT, result: { message: error.message, errors: error.errors } })
        }
    }
}

//喜欢
export const favoriteArticle = (slug) => {
    return async (dispatch) => {
        try {
            // 网络请求
            const result = await request.article.favorite(slug)
            // console.log('喜欢文章-action', result);
            dispatch({ type: constant.ARTICLE_FAVORITE_RESULT, result })
        } catch (error) { // 错误
            // console.log(error);
            dispatch({ type: constant.ARTICLE_FAVORITE_RESULT, result: { message: error.message, errors: error.errors } })
        }
    }
}